/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CrossroadGUI.java
 *
 * Created on Mar 25, 2012, 4:45:06 PM
 */
package rs.etf.mips.crossroad.ui;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import javax.imageio.ImageIO;
import javax.swing.JLabel;
import rs.etf.mips.crossroad.IDraw;
import rs.etf.mips.crossroad.logic.Crossroad;
import rs.etf.mips.crossroad.logic.Light;
import rs.etf.mips.crossroad.logic.PedestrianSemaphore;
import rs.etf.mips.crossroad.logic.Street;
import rs.etf.mips.crossroad.logic.StreetDirection;
import rs.etf.mips.crossroad.logic.Vehicle;
import rs.etf.mips.crossroad.resources.ResourceManager;

/**
 *
 * @author kasiopeja
 */
public class CrossroadGUI extends javax.swing.JPanel implements IDraw {

    private static CrossroadGUI singleton = null;
    private static boolean i;
    HashMap<Vehicle, JLabel> cars = new HashMap();
    List<JLabel> carLabels ;
    List<JLabel> semLabels;
    private JLabel semafor;
    private ResourceManager resManager;

    private static int pedSemWidth = 10;
    private static int pedSemHeight = 20;
    private static int pedSemDigWidth = 10;
    private static int pedSemDigHeight = 20;
    
    private JLabel pedLightSouth1, pedLightSouth2, pedDigitSouth11, pedDigitSouth12, pedDigitSouth21, pedDigitSouth22;
    private JLabel pedLightNorth1, pedLightNorth2, pedDigitNorth11, pedDigitNorth12, pedDigitNorth21, pedDigitNorth22;
    private JLabel pedLightWest1, pedLightWest2, pedDigitWest11, pedDigitWest12, pedDigitWest21, pedDigitWest22;
    private JLabel pedLightEast1, pedLightEast2, pedDigitEast11, pedDigitEast12, pedDigitEast21, pedDigitEast22;
        
    public static CrossroadGUI getInstance() {
        if (singleton == null) {
            singleton = new CrossroadGUI();
            i = false;
        }
        return singleton;
    }

    /** Creates new form CrossroadGUI */
    public CrossroadGUI() {
        initComponents();

        // FIXME
        resManager = ResourceManager.getInstance();
        
        semLabels = new ArrayList<>(12);
        
        semLabels.add(semLabelWest3); semLabels.add(semLabelWest4); semLabels.add(semLabelWest5);
        semLabels.add(semLabelEast3); semLabels.add(semLabelEast4); semLabels.add(semLabelEast5); 
        semLabels.add(semLabelNorth3); semLabels.add(semLabelNorth4); semLabels.add(semLabelNorth5);  
        semLabels.add(semLabelSouth3); semLabels.add(semLabelSouth4); semLabels.add(semLabelSouth5); 
        
        setIntitialSem(semLabels);
    
        carLabels = new ArrayList<>(20);
        carLabels.add(jLabel2);  carLabels.add(jLabel3);  carLabels.add(jLabel4); carLabels.add(jLabel5); 
        carLabels.add(jLabel6);  carLabels.add(jLabel7);  carLabels.add(jLabel8); carLabels.add(jLabel12);
        carLabels.add(jLabel9);  carLabels.add(jLabel10); carLabels.add(jLabel11); carLabels.add(jLabel13);
        
        pedLightSouth1 = new JLabel();
        pedLightSouth2 = new JLabel();
        pedDigitSouth11 = new JLabel();
        pedDigitSouth12 = new JLabel();
        pedDigitSouth21 = new JLabel();
        pedDigitSouth22 = new JLabel();
        pedLightSouth1.setBounds(240, 530, pedSemWidth, pedSemHeight);
        pedLightSouth2.setBounds(pedLightSouth1.getX()+245, pedLightSouth1.getY(), pedSemWidth, pedSemHeight);
        pedDigitSouth11.setBounds(pedLightSouth1.getX()-2*(pedSemDigWidth+5), pedLightSouth1.getY(), pedSemDigWidth, pedSemDigHeight);
        pedDigitSouth12.setBounds(pedLightSouth1.getX()-(pedSemDigWidth+5), pedLightSouth1.getY(), pedSemDigWidth, pedSemDigHeight);
        pedDigitSouth21.setBounds(pedLightSouth2.getX()+pedSemWidth+5, pedLightSouth1.getY(), pedSemDigWidth, pedSemDigHeight);
        pedDigitSouth22.setBounds(pedLightSouth2.getX()+pedSemWidth+(pedSemDigWidth+10), pedLightSouth1.getY(), pedSemDigWidth, pedSemDigHeight);
        jLayeredPane1.add(pedLightSouth1);
        jLayeredPane1.add(pedLightSouth2);
        jLayeredPane1.add(pedDigitSouth11);
        jLayeredPane1.add(pedDigitSouth12);
        jLayeredPane1.add(pedDigitSouth21);
        jLayeredPane1.add(pedDigitSouth22);
        
        
        pedLightNorth1 = new JLabel();
        pedLightNorth2 = new JLabel();
        pedDigitNorth11 = new JLabel();
        pedDigitNorth12 = new JLabel();
        pedDigitNorth21 = new JLabel();
        pedDigitNorth22 = new JLabel();
        pedLightNorth1.setBounds(pedLightSouth1.getX(), 180, pedSemWidth, pedSemHeight);
        pedLightNorth2.setBounds(pedLightNorth1.getX()+245, pedLightNorth1.getY(), pedSemWidth, pedSemHeight);
        pedDigitNorth11.setBounds(pedLightNorth1.getX()-2*(pedSemDigWidth+5), pedLightNorth1.getY(), pedSemDigWidth, pedSemDigHeight);
        pedDigitNorth12.setBounds(pedLightNorth1.getX()-(pedSemDigWidth+5), pedLightNorth1.getY(), pedSemDigWidth, pedSemDigHeight);
        pedDigitNorth21.setBounds(pedLightNorth2.getX()+pedSemWidth+5, pedLightNorth1.getY(), pedSemDigWidth, pedSemDigHeight);
        pedDigitNorth22.setBounds(pedLightNorth2.getX()+pedSemWidth+(pedSemDigWidth+10), pedLightNorth1.getY(), pedSemDigWidth, pedSemDigHeight);
        jLayeredPane1.add(pedLightNorth1);
        jLayeredPane1.add(pedLightNorth2);
        jLayeredPane1.add(pedDigitNorth11);
        jLayeredPane1.add(pedDigitNorth12);
        jLayeredPane1.add(pedDigitNorth21);
        jLayeredPane1.add(pedDigitNorth22);
        
        pedLightWest1 = new JLabel();
        pedLightWest2 = new JLabel();
        pedDigitWest11 = new JLabel();
        pedDigitWest12 = new JLabel();
        pedDigitWest21 = new JLabel();
        pedDigitWest22 = new JLabel();
        pedLightWest1.setBounds(210, 230, pedSemWidth, pedSemHeight);
        pedLightWest2.setBounds(pedLightWest1.getX(), pedLightNorth1.getY()+310, pedSemWidth, pedSemHeight);
        pedDigitWest11.setBounds(pedLightWest1.getX()-2*(pedSemDigWidth+5), pedLightWest1.getY(), pedSemDigWidth, pedSemDigHeight);
        pedDigitWest12.setBounds(pedLightWest1.getX()-(pedSemDigWidth+5), pedLightWest1.getY(), pedSemDigWidth, pedSemDigHeight);
        pedDigitWest21.setBounds(pedLightWest2.getX()-2*(pedSemDigWidth+5), pedLightWest2.getY(), pedSemDigWidth, pedSemDigHeight);
        pedDigitWest22.setBounds(pedLightWest2.getX()-(pedSemDigWidth+5), pedLightWest2.getY(), pedSemDigWidth, pedSemDigHeight);
        jLayeredPane1.add(pedLightWest1);
        jLayeredPane1.add(pedLightWest2);
        jLayeredPane1.add(pedDigitWest11);
        jLayeredPane1.add(pedDigitWest12);
        jLayeredPane1.add(pedDigitWest21);
        jLayeredPane1.add(pedDigitWest22);
        
        pedLightEast1 = new JLabel();
        pedLightEast2 = new JLabel();
        pedDigitEast11 = new JLabel();
        pedDigitEast12 = new JLabel();
        pedDigitEast21 = new JLabel();
        pedDigitEast22 = new JLabel();
        pedLightEast1.setBounds(pedLightWest1.getX()+300, pedLightWest1.getY(), pedSemWidth, pedSemHeight);
        pedLightEast2.setBounds(pedLightEast1.getX(), pedLightWest2.getY(), pedSemWidth, pedSemHeight);
        pedDigitEast11.setBounds(pedLightEast1.getX()+pedSemWidth+5, pedLightEast1.getY(), pedSemDigWidth, pedSemDigHeight);
        pedDigitEast12.setBounds(pedLightEast1.getX()+pedSemWidth+(pedSemDigWidth+10), pedLightEast1.getY(), pedSemDigWidth, pedSemDigHeight);
        pedDigitEast21.setBounds(pedLightEast2.getX()+pedSemWidth+5, pedLightEast2.getY(), pedSemDigWidth, pedSemDigHeight);
        pedDigitEast22.setBounds(pedLightEast2.getX()+pedSemWidth+(pedSemDigWidth+10), pedLightEast2.getY(), pedSemDigWidth, pedSemDigHeight);
        jLayeredPane1.add(pedLightEast1);
        jLayeredPane1.add(pedLightEast2);
        jLayeredPane1.add(pedDigitEast11);
        jLayeredPane1.add(pedDigitEast12);
        jLayeredPane1.add(pedDigitEast21);
        jLayeredPane1.add(pedDigitEast22);
        
//        while(carLabels.iterator().hasNext()){
//         carLabels.iterator().next().setVisible(false);   
//        }
        for (int i=0; i<12; i++){
            carLabels.get(i).setVisible(false);
        }
    }
    
    @Override //ovo je inicijalno crtanje
    protected void paintComponent(Graphics g) {
        //   jLabel2.setLocation(1 , 37);
        super.paintComponent(g);

    }

    public void moveJLabel2() {
        jLabel2.setLocation(jLabel2.getLocation().x + 1, jLabel2.getLocation().y);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jLayeredPane1 = new javax.swing.JLayeredPane();
        jLabel3 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();
        semLabelWest3 = new javax.swing.JLabel();
        semLabelWest4 = new javax.swing.JLabel();
        semLabelWest5 = new javax.swing.JLabel();
        semLabelNorth3 = new javax.swing.JLabel();
        semLabelNorth4 = new javax.swing.JLabel();
        semLabelNorth5 = new javax.swing.JLabel();
        semLabelSouth3 = new javax.swing.JLabel();
        semLabelSouth4 = new javax.swing.JLabel();
        semLabelSouth5 = new javax.swing.JLabel();
        semLabelEast3 = new javax.swing.JLabel();
        semLabelEast4 = new javax.swing.JLabel();
        semLabelEast5 = new javax.swing.JLabel();
        westPress1 = new javax.swing.JLabel();
        westPress2 = new javax.swing.JLabel();
        westPress3 = new javax.swing.JLabel();
        westPress4 = new javax.swing.JLabel();
        westPress5 = new javax.swing.JLabel();
        westPress6 = new javax.swing.JLabel();
        westPress7 = new javax.swing.JLabel();
        westPress8 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();

        setAlignmentX(0.0F);
        setAlignmentY(0.0F);
        setMaximumSize(new java.awt.Dimension(722, 722));
        setMinimumSize(new java.awt.Dimension(722, 722));
        setName(""); // NOI18N
        setPreferredSize(new java.awt.Dimension(722, 722));
        setLayout(new java.awt.GridBagLayout());

        jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/rs/etf/mips/crossroad/resources/Images/cars/blck.png"))); // NOI18N
        jLabel3.setAlignmentX(15.0F);
        jLabel3.setAlignmentY(15.0F);
        jLabel3.setBounds(500, 160, 30, 30);
        jLayeredPane1.add(jLabel3, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/rs/etf/mips/crossroad/resources/Images/cars/blck.png"))); // NOI18N
        jLabel2.setAlignmentX(15.0F);
        jLabel2.setAlignmentY(15.0F);
        jLabel2.setBounds(0, 380, 30, 30);
        jLayeredPane1.add(jLabel2, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel4.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/rs/etf/mips/crossroad/resources/Images/cars/blck.png"))); // NOI18N
        jLabel4.setAlignmentX(15.0F);
        jLabel4.setAlignmentY(15.0F);
        jLabel4.setBounds(450, 70, 30, 30);
        jLayeredPane1.add(jLabel4, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel5.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/rs/etf/mips/crossroad/resources/Images/cars/blck.png"))); // NOI18N
        jLabel5.setAlignmentX(15.0F);
        jLabel5.setAlignmentY(15.0F);
        jLabel5.setBounds(120, 190, 30, 30);
        jLayeredPane1.add(jLabel5, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel6.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel6.setIcon(new javax.swing.ImageIcon(getClass().getResource("/rs/etf/mips/crossroad/resources/Images/cars/blck.png"))); // NOI18N
        jLabel6.setAlignmentX(15.0F);
        jLabel6.setAlignmentY(15.0F);
        jLabel6.setBounds(190, 110, 30, 30);
        jLayeredPane1.add(jLabel6, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel7.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel7.setIcon(new javax.swing.ImageIcon(getClass().getResource("/rs/etf/mips/crossroad/resources/Images/cars/blck.png"))); // NOI18N
        jLabel7.setAlignmentX(15.0F);
        jLabel7.setAlignmentY(15.0F);
        jLabel7.setBounds(90, 90, 30, 30);
        jLayeredPane1.add(jLabel7, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel8.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel8.setIcon(new javax.swing.ImageIcon(getClass().getResource("/rs/etf/mips/crossroad/resources/Images/cars/blck.png"))); // NOI18N
        jLabel8.setAlignmentX(15.0F);
        jLabel8.setAlignmentY(15.0F);
        jLabel8.setBounds(130, 150, 30, 30);
        jLayeredPane1.add(jLabel8, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel9.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel9.setIcon(new javax.swing.ImageIcon(getClass().getResource("/rs/etf/mips/crossroad/resources/Images/cars/blck.png"))); // NOI18N
        jLabel9.setAlignmentX(15.0F);
        jLabel9.setAlignmentY(15.0F);
        jLabel9.setBounds(110, 40, 30, 30);
        jLayeredPane1.add(jLabel9, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel10.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel10.setIcon(new javax.swing.ImageIcon(getClass().getResource("/rs/etf/mips/crossroad/resources/Images/cars/blck.png"))); // NOI18N
        jLabel10.setAlignmentX(15.0F);
        jLabel10.setAlignmentY(15.0F);
        jLabel10.setBounds(40, 150, 30, 30);
        jLayeredPane1.add(jLabel10, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel11.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel11.setIcon(new javax.swing.ImageIcon(getClass().getResource("/rs/etf/mips/crossroad/resources/Images/cars/blck.png"))); // NOI18N
        jLabel11.setAlignmentX(15.0F);
        jLabel11.setAlignmentY(15.0F);
        jLabel11.setBounds(210, 70, 30, 30);
        jLayeredPane1.add(jLabel11, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel12.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel12.setIcon(new javax.swing.ImageIcon(getClass().getResource("/rs/etf/mips/crossroad/resources/Images/cars/blck.png"))); // NOI18N
        jLabel12.setAlignmentX(15.0F);
        jLabel12.setAlignmentY(15.0F);
        jLabel12.setBounds(40, 70, 30, 30);
        jLayeredPane1.add(jLabel12, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel13.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel13.setIcon(new javax.swing.ImageIcon(getClass().getResource("/rs/etf/mips/crossroad/resources/Images/cars/blck.png"))); // NOI18N
        jLabel13.setAlignmentX(15.0F);
        jLabel13.setAlignmentY(15.0F);
        jLabel13.setBounds(150, 70, 30, 30);
        jLayeredPane1.add(jLabel13, javax.swing.JLayeredPane.DEFAULT_LAYER);
        semLabelWest3.setBounds(590, 140, 0, 0);
        jLayeredPane1.add(semLabelWest3, javax.swing.JLayeredPane.DEFAULT_LAYER);
        semLabelWest4.setBounds(600, 160, 0, 0);
        jLayeredPane1.add(semLabelWest4, javax.swing.JLayeredPane.DEFAULT_LAYER);
        semLabelWest5.setBounds(600, 180, 0, 0);
        jLayeredPane1.add(semLabelWest5, javax.swing.JLayeredPane.DEFAULT_LAYER);
        semLabelNorth3.setBounds(550, 110, 0, 0);
        jLayeredPane1.add(semLabelNorth3, javax.swing.JLayeredPane.DEFAULT_LAYER);
        semLabelNorth4.setBounds(560, 140, 0, 0);
        jLayeredPane1.add(semLabelNorth4, javax.swing.JLayeredPane.DEFAULT_LAYER);
        semLabelNorth5.setBounds(560, 110, 0, 0);
        jLayeredPane1.add(semLabelNorth5, javax.swing.JLayeredPane.DEFAULT_LAYER);
        semLabelSouth3.setBounds(550, 170, 0, 0);
        jLayeredPane1.add(semLabelSouth3, javax.swing.JLayeredPane.DEFAULT_LAYER);
        semLabelSouth4.setBounds(550, 210, 0, 0);
        jLayeredPane1.add(semLabelSouth4, javax.swing.JLayeredPane.DEFAULT_LAYER);
        semLabelSouth5.setBounds(550, 130, 0, 0);
        jLayeredPane1.add(semLabelSouth5, javax.swing.JLayeredPane.DEFAULT_LAYER);
        semLabelEast3.setBounds(560, 190, 0, 0);
        jLayeredPane1.add(semLabelEast3, javax.swing.JLayeredPane.DEFAULT_LAYER);
        semLabelEast4.setBounds(570, 210, 0, 0);
        jLayeredPane1.add(semLabelEast4, javax.swing.JLayeredPane.DEFAULT_LAYER);
        semLabelEast5.setBounds(560, 160, 0, 0);
        jLayeredPane1.add(semLabelEast5, javax.swing.JLayeredPane.DEFAULT_LAYER);

        westPress1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/rs/etf/mips/crossroad/resources/Images/Press.png"))); // NOI18N
        westPress1.setToolTipText("Prijavi se za prelazak");
        westPress1.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        westPress1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                westPress1MouseClicked(evt);
            }
        });
        westPress1.setBounds(470, 490, 16, 16);
        jLayeredPane1.add(westPress1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        westPress2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/rs/etf/mips/crossroad/resources/Images/Press.png"))); // NOI18N
        westPress2.setToolTipText("Prijavi se za prelazak");
        westPress2.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        westPress2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                westPress2MouseClicked(evt);
            }
        });
        westPress2.setBounds(220, 240, 16, 16);
        jLayeredPane1.add(westPress2, javax.swing.JLayeredPane.DEFAULT_LAYER);

        westPress3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/rs/etf/mips/crossroad/resources/Images/Press.png"))); // NOI18N
        westPress3.setToolTipText("Prijavi se za prelazak");
        westPress3.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        westPress3.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                westPress3MouseClicked(evt);
            }
        });
        westPress3.setBounds(220, 470, 16, 16);
        jLayeredPane1.add(westPress3, javax.swing.JLayeredPane.DEFAULT_LAYER);

        westPress4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/rs/etf/mips/crossroad/resources/Images/Press.png"))); // NOI18N
        westPress4.setToolTipText("Prijavi se za prelazak");
        westPress4.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        westPress4.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                westPress4MouseClicked(evt);
            }
        });
        westPress4.setBounds(490, 470, 16, 16);
        jLayeredPane1.add(westPress4, javax.swing.JLayeredPane.DEFAULT_LAYER);

        westPress5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/rs/etf/mips/crossroad/resources/Images/Press.png"))); // NOI18N
        westPress5.setToolTipText("Prijavi se za prelazak");
        westPress5.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        westPress5.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                westPress5MouseClicked(evt);
            }
        });
        westPress5.setBounds(490, 240, 16, 16);
        jLayeredPane1.add(westPress5, javax.swing.JLayeredPane.DEFAULT_LAYER);

        westPress6.setIcon(new javax.swing.ImageIcon(getClass().getResource("/rs/etf/mips/crossroad/resources/Images/Press.png"))); // NOI18N
        westPress6.setToolTipText("Prijavi se za prelazak");
        westPress6.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        westPress6.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                westPress6MouseClicked(evt);
            }
        });
        westPress6.setBounds(470, 220, 16, 16);
        jLayeredPane1.add(westPress6, javax.swing.JLayeredPane.DEFAULT_LAYER);

        westPress7.setIcon(new javax.swing.ImageIcon(getClass().getResource("/rs/etf/mips/crossroad/resources/Images/Press.png"))); // NOI18N
        westPress7.setToolTipText("Prijavi se za prelazak");
        westPress7.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        westPress7.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                westPress7MouseClicked(evt);
            }
        });
        westPress7.setBounds(240, 220, 16, 16);
        jLayeredPane1.add(westPress7, javax.swing.JLayeredPane.DEFAULT_LAYER);

        westPress8.setIcon(new javax.swing.ImageIcon(getClass().getResource("/rs/etf/mips/crossroad/resources/Images/Press.png"))); // NOI18N
        westPress8.setToolTipText("Prijavi se za prelazak");
        westPress8.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        westPress8.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                westPress8MouseClicked(evt);
            }
        });
        westPress8.setBounds(240, 490, 16, 16);
        jLayeredPane1.add(westPress8, javax.swing.JLayeredPane.DEFAULT_LAYER);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.ipadx = 722;
        gridBagConstraints.ipady = 722;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 1);
        add(jLayeredPane1, gridBagConstraints);
        jLayeredPane1.getAccessibleContext().setAccessibleName("");

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/rs/etf/mips/crossroad/resources/Images/crossroad_plainSQ.png"))); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 1);
        add(jLabel1, gridBagConstraints);

        getAccessibleContext().setAccessibleDescription("");
    }// </editor-fold>//GEN-END:initComponents

    private void westPress7MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_westPress7MouseClicked
        Crossroad crossroad = CrossroadFrame.getInstance().getCrossroad();
        crossroad.getStreet(StreetDirection.North).setPedestrianArrived(true);
    }//GEN-LAST:event_westPress7MouseClicked

    private void westPress6MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_westPress6MouseClicked
        Crossroad crossroad = CrossroadFrame.getInstance().getCrossroad();
        crossroad.getStreet(StreetDirection.North).setPedestrianArrived(true);
    }//GEN-LAST:event_westPress6MouseClicked

    private void westPress2MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_westPress2MouseClicked
        Crossroad crossroad = CrossroadFrame.getInstance().getCrossroad();
        crossroad.getStreet(StreetDirection.West).setPedestrianArrived(true);
    }//GEN-LAST:event_westPress2MouseClicked

    private void westPress3MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_westPress3MouseClicked
        Crossroad crossroad = CrossroadFrame.getInstance().getCrossroad();
        crossroad.getStreet(StreetDirection.West).setPedestrianArrived(true);
    }//GEN-LAST:event_westPress3MouseClicked

    private void westPress8MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_westPress8MouseClicked
        Crossroad crossroad = CrossroadFrame.getInstance().getCrossroad();
        crossroad.getStreet(StreetDirection.South).setPedestrianArrived(true);
    }//GEN-LAST:event_westPress8MouseClicked

    private void westPress1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_westPress1MouseClicked
        Crossroad crossroad = CrossroadFrame.getInstance().getCrossroad();
        crossroad.getStreet(StreetDirection.South).setPedestrianArrived(true);
    }//GEN-LAST:event_westPress1MouseClicked

    private void westPress5MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_westPress5MouseClicked
        Crossroad crossroad = CrossroadFrame.getInstance().getCrossroad();
        crossroad.getStreet(StreetDirection.East).setPedestrianArrived(true);
    }//GEN-LAST:event_westPress5MouseClicked

    private void westPress4MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_westPress4MouseClicked
        Crossroad crossroad = CrossroadFrame.getInstance().getCrossroad();
        crossroad.getStreet(StreetDirection.East).setPedestrianArrived(true);
    }//GEN-LAST:event_westPress4MouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JLayeredPane jLayeredPane1;
    private javax.swing.JLabel semLabelEast3;
    private javax.swing.JLabel semLabelEast4;
    private javax.swing.JLabel semLabelEast5;
    private javax.swing.JLabel semLabelNorth3;
    private javax.swing.JLabel semLabelNorth4;
    private javax.swing.JLabel semLabelNorth5;
    private javax.swing.JLabel semLabelSouth3;
    private javax.swing.JLabel semLabelSouth4;
    private javax.swing.JLabel semLabelSouth5;
    private javax.swing.JLabel semLabelWest3;
    private javax.swing.JLabel semLabelWest4;
    private javax.swing.JLabel semLabelWest5;
    private javax.swing.JLabel westPress1;
    private javax.swing.JLabel westPress2;
    private javax.swing.JLabel westPress3;
    private javax.swing.JLabel westPress4;
    private javax.swing.JLabel westPress5;
    private javax.swing.JLabel westPress6;
    private javax.swing.JLabel westPress7;
    private javax.swing.JLabel westPress8;
    // End of variables declaration//GEN-END:variables

    protected void drawCarPosition(int LineNum, StreetDirection dir, int x, int y, Vehicle v) {
        int posX = x-15;
        int posY = y-15;
        int temp;
        switch (LineNum){
            case 0:
                if (posX>=220|| posY>=220){ cars.get(v).setVisible(false); break;}
                if (dir == StreetDirection.West) {
                    posX = 257 - posX; posY = posY + 262;
                    cars.get(v).setLocation(posX, posY); break;
                    
                }
                if (dir == StreetDirection.East){
                    posX = posX + 463; posY = posY + 262 + 5*32+1;
                    cars.get(v).setLocation(posX, posY); break;
                }
                if (dir == StreetDirection.South){
                    temp = posX;
                    posX = posY + 262; posY = 463 + temp; // vidi koliko pomerash
                    cars.get(v).setLocation(posX, posY); break;
                }
                if (dir == StreetDirection.North){
                    temp = posX;
                    posX = posY + 262 + 5*32+1; posY = 262 - temp; // vidi koliko pomerash
                    cars.get(v).setLocation(posX, posY); break;
                }             
                
            case 1:
                if (posX>=220|| posY>=220){ cars.get(v).setVisible(false); break;}
                if (dir == StreetDirection.West) {
                    posX = 257 - posX; posY = posY + 262 + 32;
                    cars.get(v).setLocation(posX, posY); break;
                }
                if (dir == StreetDirection.East){
                    posX = posX + 463; posY = posY + 262 + 4*32+1;
                    cars.get(v).setLocation(posX, posY); break;
                }
                if (dir == StreetDirection.South){
                    temp = posX;
                    posX = posY + 262 + 32; posY = 463 + temp; // vidi koliko pomerash
                    cars.get(v).setLocation(posX, posY); break;
                }
                if (dir == StreetDirection.North){
                    temp = posX;
                    posX = posY + 262 + 4*32+1; posY = 262 - temp; // vidi koliko pomerash
                    cars.get(v).setLocation(posX, posY); break;
                }
            case 2:
                if (posX>=220|| posY>=220){ cars.get(v).setVisible(false); break;}
                if (dir == StreetDirection.West) {
                    posX = 257 - posX; posY = posY + 262 + 2*32;
                    cars.get(v).setLocation(posX, posY); break;
                }
                if (dir == StreetDirection.East){
                    posX = posX + 463; posY = posY + 262 + 3*32 + 1;
                    cars.get(v).setLocation(posX, posY); break;
                }
                if (dir == StreetDirection.South){
                    temp = posX;
                    posX = posY + 262 + 2*32; posY = 463 + temp; // vidi koliko pomerash
                    cars.get(v).setLocation(posX, posY); break;
                }
                if (dir == StreetDirection.North){
                    temp = posX;
                    posX = posY + 262 + 3*32+1; posY = 262 - temp; // vidi koliko pomerash
                    cars.get(v).setLocation(posX, posY); break;
                }
            case 3:
                
                if (dir == StreetDirection.West) {
                    posY = posY + 262 + 3*32+1;
                    cars.get(v).setLocation(posX, posY); break;
                }
                if (dir == StreetDirection.East){
                    posX = 700 - posX; posY = posY + 262 + 2*32;
                    cars.get(v).setLocation(posX, posY); break;
                }
                if (dir == StreetDirection.South){
                    temp = posX;
                    posX = posY + 266 + 3*32+1; posY = 700 - temp; // vidi koliko pomerash
                    cars.get(v).setLocation(posX, posY); break;
                }
                if (dir == StreetDirection.North){
                    temp = posX;
                    posX = posY + 262 + 2*32; posY = temp; // vidi koliko pomerash
                    cars.get(v).setLocation(posX, posY); break;
                }
            case 4:
                
                if (dir == StreetDirection.West) {
                     posY = posY + 262 + 4*32+1;
                     cars.get(v).setLocation(posX, posY); break;
                }
                if (dir == StreetDirection.East){
                    posX = 700 - posX; posY = posY + 262 + 32;
                    cars.get(v).setLocation(posX, posY); break;
                }
                if (dir == StreetDirection.South){
                    temp = posX;
                    posX = posY + 262 + 4*32+1; posY = 700 - temp; // vidi koliko pomerash
                    cars.get(v).setLocation(posX, posY); break;
                }
                if (dir == StreetDirection.North){
                    temp = posX;
                    posX = posY + 262 + 32; posY = temp; // vidi koliko pomerash
                    cars.get(v).setLocation(posX, posY); break;
                }
            case 5:
                
                if (dir == StreetDirection.West) {
                    posY = posY + 262 + 5*32+1;
                    cars.get(v).setLocation(posX, posY); break;
                }
                if (dir == StreetDirection.East){
                    posX = 700 - posX; posY = posY + 262;
                    cars.get(v).setLocation(posX, posY); break;
                }
                if (dir == StreetDirection.South){
                    temp = posX;
                    posX = posY + 262 + 5*32+1; posY = 700 - temp; // vidi koliko pomerash
                    cars.get(v).setLocation(posX, posY); break;
                }
                if (dir == StreetDirection.North){
                    temp = posX;
                    posX = posY + 262; posY = temp; // vidi koliko pomerash
                    cars.get(v).setLocation(posX, posY); break;
                }
        }
        
    }

    private void drawDir(Street street, StreetDirection dir) {
        for (int i = 0; i < 6; i++) {
            Iterator<Vehicle> iter = street.getLine(i).iterator();
            while (iter.hasNext()) {
                Vehicle v = iter.next();
                drawCarPosition(i, dir, (int) v.getX(), (int) v.getY(), v); 
                //cars.get(v).setLocation((int) v.getX(), (int) v.getY());               
                break;
            }
        }
    }
    
    private void drawSemaphores(Crossroad crossroad) {
        resManager.setSemaphore(crossroad.getStreet(StreetDirection.West).getLine(3).getSemaphore(), semLabelWest3);
        resManager.setSemaphore(crossroad.getStreet(StreetDirection.West).getLine(4).getSemaphore(), semLabelWest4);
        resManager.setSemaphore(crossroad.getStreet(StreetDirection.West).getLine(5).getSemaphore(), semLabelWest5);
        resManager.setSemaphore(crossroad.getStreet(StreetDirection.East).getLine(3).getSemaphore(), semLabelEast3);
        resManager.setSemaphore(crossroad.getStreet(StreetDirection.East).getLine(4).getSemaphore(), semLabelEast4);
        resManager.setSemaphore(crossroad.getStreet(StreetDirection.East).getLine(5).getSemaphore(), semLabelEast5);
        resManager.setSemaphore(crossroad.getStreet(StreetDirection.North).getLine(3).getSemaphore(), semLabelNorth3);
        resManager.setSemaphore(crossroad.getStreet(StreetDirection.North).getLine(4).getSemaphore(), semLabelNorth4);
        resManager.setSemaphore(crossroad.getStreet(StreetDirection.North).getLine(5).getSemaphore(), semLabelNorth5);
        resManager.setSemaphore(crossroad.getStreet(StreetDirection.South).getLine(3).getSemaphore(), semLabelSouth3);
        resManager.setSemaphore(crossroad.getStreet(StreetDirection.South).getLine(4).getSemaphore(), semLabelSouth4);
        resManager.setSemaphore(crossroad.getStreet(StreetDirection.South).getLine(5).getSemaphore(), semLabelSouth5);
    }
    

    private void drawPedestrainSemaphores(Crossroad crossroad){
    	PedestrianSemaphore pedSem;
    	
    	pedSem = crossroad.getStreet(StreetDirection.South).getPedestrianSemaphore();
    	resManager.setPedestrianSemaphore(pedSem.getSemaforeLight(), pedLightSouth1);
    	resManager.setPedestrianSemaphore(pedSem.getSemaforeLight(), pedLightSouth2);
    	resManager.setDisplaySemaphore((int)(pedSem.getTimeLeft()/10000)%10, pedSem.getDisplayLight(), pedDigitSouth11);
    	resManager.setDisplaySemaphore((int)(pedSem.getTimeLeft()/1000)%10, pedSem.getDisplayLight(), pedDigitSouth12);
    	resManager.setDisplaySemaphore((int)(pedSem.getTimeLeft()/10000)%10, pedSem.getDisplayLight(), pedDigitSouth21);
    	resManager.setDisplaySemaphore((int)(pedSem.getTimeLeft()/1000)%10, pedSem.getDisplayLight(), pedDigitSouth22);
    	
    	pedSem = crossroad.getStreet(StreetDirection.North).getPedestrianSemaphore();
    	resManager.setPedestrianSemaphore(pedSem.getSemaforeLight(), pedLightNorth1);
    	resManager.setPedestrianSemaphore(pedSem.getSemaforeLight(), pedLightNorth2);
    	resManager.setDisplaySemaphore((int)(pedSem.getTimeLeft()/10000)%10, pedSem.getDisplayLight(), pedDigitNorth11);
    	resManager.setDisplaySemaphore((int)(pedSem.getTimeLeft()/1000)%10, pedSem.getDisplayLight(), pedDigitNorth12);
    	resManager.setDisplaySemaphore((int)(pedSem.getTimeLeft()/10000)%10, pedSem.getDisplayLight(), pedDigitNorth21);
    	resManager.setDisplaySemaphore((int)(pedSem.getTimeLeft()/1000)%10, pedSem.getDisplayLight(), pedDigitNorth22);
    	
    	pedSem = crossroad.getStreet(StreetDirection.East).getPedestrianSemaphore();
    	resManager.setPedestrianSemaphore(pedSem.getSemaforeLight(), pedLightEast1);
    	resManager.setPedestrianSemaphore(pedSem.getSemaforeLight(), pedLightEast2);
    	resManager.setDisplaySemaphore((int)(pedSem.getTimeLeft()/10000)%10, pedSem.getDisplayLight(), pedDigitEast11);
    	resManager.setDisplaySemaphore((int)(pedSem.getTimeLeft()/1000)%10, pedSem.getDisplayLight(), pedDigitEast12);
    	resManager.setDisplaySemaphore((int)(pedSem.getTimeLeft()/10000)%10, pedSem.getDisplayLight(), pedDigitEast21);
    	resManager.setDisplaySemaphore((int)(pedSem.getTimeLeft()/1000)%10, pedSem.getDisplayLight(), pedDigitEast22);
    	
    	pedSem = crossroad.getStreet(StreetDirection.West).getPedestrianSemaphore();
    	resManager.setPedestrianSemaphore(pedSem.getSemaforeLight(), pedLightWest1);
    	resManager.setPedestrianSemaphore(pedSem.getSemaforeLight(), pedLightWest2);
    	resManager.setDisplaySemaphore((int)(pedSem.getTimeLeft()/10000)%10, pedSem.getDisplayLight(), pedDigitWest11);
    	resManager.setDisplaySemaphore((int)(pedSem.getTimeLeft()/1000)%10, pedSem.getDisplayLight(), pedDigitWest12);
    	resManager.setDisplaySemaphore((int)(pedSem.getTimeLeft()/10000)%10, pedSem.getDisplayLight(), pedDigitWest21);
    	resManager.setDisplaySemaphore((int)(pedSem.getTimeLeft()/1000)%10, pedSem.getDisplayLight(), pedDigitWest22);
    }
    
    @Override
    public void draw(Crossroad crossroad) {

        drawDir(crossroad.getStreet(StreetDirection.North), StreetDirection.North);
        drawDir(crossroad.getStreet(StreetDirection.East), StreetDirection.East);
        drawDir(crossroad.getStreet(StreetDirection.West), StreetDirection.West);
        drawDir(crossroad.getStreet(StreetDirection.South), StreetDirection.South);
        drawSemaphores(crossroad);
        drawPedestrainSemaphores(crossroad);
        Iterator<Vehicle> iter2 = crossroad.getCenter().iterator();
                while(iter2.hasNext()){
                        Vehicle v1 = iter2.next();
                        cars.get(v1).setLocation((int)v1.getX()+262-15,(int)v1.getY()+262-15);  
                                        
                     }
    }

    void addVehicles(Crossroad crossroad) {
       //ne treba
    }
    void addVehicles (Crossroad crossroad, StreetDirection dir, int LineNum){
        Vehicle v = new Vehicle();
        crossroad.getStreet(dir).getLine(LineNum).addVehicle(v);
        cars.put(v, carLabels.get(0));
        drawCarPosition(LineNum, dir, 15, 15, v);
        carLabels.get(0).setVisible(true);
        carLabels.remove(0);
       
    }

    private void setIntitialSem(List<JLabel> semLabels) {
        for (int i=0; i<12; i++){
            JLabel sem = semLabels.get(i);
            sem.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
            URL imageURL = getClass().getResource("/rs/etf/mips/crossroad/resources/Images/Semaphores/traffic-semaphore-red-light-md.png");
            BufferedImage buffImg =null;
            try {
                buffImg = ImageIO.read(imageURL);
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            if (i/3 == 0) sem.setBounds(230*(i/3+1), 365+(35*(i%3)), 30, 25);
            else if (i/3 == 1) sem.setBounds(230*(i/3+1), 340-(35*(i%3)), 30, 25);
            else if (i/3 == 2) sem.setBounds(330-(35*(i%3)), 230, 30, 25);
            else sem.setBounds(365+(35*(i%3)), 470, 30, 25);
            
            sem.setIcon(new javax.swing.ImageIcon(buffImg.getScaledInstance(sem.getWidth(), sem.getHeight(), Image.SCALE_SMOOTH))); // NOI18N  
            }
    }

   

    
}
